package com.suzhe.ymall.common.interceptor;

import com.suzhe.ymall.common.utils.YMConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
@Configuration
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader(YMConstant.ACCESS_TOKEN);
        String source = request.getHeader(YMConstant.REQUEST_SOURCE);
        log.debug("FeignConfiguration token:{},source:{}",token,source);
        template.header(YMConstant.ACCESS_TOKEN, token);
        template.header(YMConstant.REQUEST_SOURCE,source);
    }
}
